package wiki;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONObject;

import com.Root;

/**
 * 
 * @author mona.chandrapal
 */
public class GetTreeData extends HttpServlet {

	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		JSONObject treedata = null;
		JSONObject module = null;
		JSONObject category = null;
		JSONObject subcategory = null;
		ArrayList modulelist = new ArrayList();

		Root root = new Root();
		ResultSet rs = null;
		ResultSet rs1 = null;
		ResultSet rs2 = null;
		PreparedStatement pst = null;
		PreparedStatement pst1 = null;
		PreparedStatement pst2 = null;
		String sql1, sql2, sql3;
		
		try {

			

			root.initConnection();
			sql1 = "Select id,sectionname from sections where parentid = 0  and deleteflag = 0 order by displayindex;";
			sql2 = "Select id,sectionname from sections where parentid = ?  and deleteflag = 0 order by displayindex;";
			sql3 = "Select id,sectionname from sections where parentid = ?  and deleteflag = 0 order by displayindex;";

			pst = root.con.prepareStatement(sql1);
			rs = pst.executeQuery();
			treedata = new JSONObject();

			while (rs.next()) {
				module = new JSONObject();
				String parentid = rs.getString("id");

				pst1 = root.con.prepareStatement(sql2);
				pst1.setString(1, parentid);
				rs1 = pst1.executeQuery();

				module.put("data", rs.getString("sectionname"));
				ArrayList categorylist = new ArrayList();

				while (rs1.next()) {
					category = new JSONObject();
					String categoryid = rs1.getString("id");
					category.put("data", rs1.getString("sectionname"));

					pst2 = root.con.prepareStatement(sql3);
					pst2.setString(1, categoryid);
					rs2 = pst2.executeQuery();
					ArrayList subcategorylist = new ArrayList();

					while (rs2.next()) {
						category.put("state", "closed");
						subcategory = new JSONObject();
						String subcategoryid = rs2.getString("id");
						subcategory.put("data", rs2.getString("sectionname"));
						subcategorylist.add(subcategory);

						JSONObject subcatattr = new JSONObject();
						subcatattr.put("id", subcategoryid);
						subcategory.put("metadata", subcatattr);

						category.put("children", subcategorylist);
					}
					JSONObject catattr = new JSONObject();
					catattr.put("id", categoryid);
					category.put("metadata", catattr);

					categorylist.add(category);

					module.put("children", categorylist);
					module.put("state", "closed");

				}

				JSONObject moduleattr = new JSONObject();
				moduleattr.put("id", parentid);
				module.put("metadata", moduleattr);
				modulelist.add(module);

			}

			// treedata.put("treedata", modulelist);

		} catch (Exception e) {
			System.out.println("Exception getting sections: " + e.toString());
		}finally{
			try {
				rs.close();
				rs1.close();
				rs2.close();
				pst.close();
				pst1.close();
				pst2.close();
				root.closeConnection();
				
			} catch (Exception e) {
			
			}
		}

		out.println(modulelist.toString());
	}

	// <editor-fold defaultstate="collapsed"
	// desc="HttpServlet methods. Click on the + sign on the left to edit the code.">

	/**
	 * Handles the HTTP <code>GET</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             if a servlet-specific error occurs
	 * @throws IOException
	 *             if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return a String containing servlet description
	 */
	@Override
	public String getServletInfo() {
		return "Short description";
	}// </editor-fold>
}
